package com.yanqu.road.dao.impl.unioncampwar;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.unioncampwar.cross.CrossUnionCampWarGroupUnionData;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UnionCampWarGroupUnionDaoImpl extends TempDataObjectDao<CrossUnionCampWarGroupUnionData> {


//    public static String getCrossUnionCampWarGroupUnionDataSql = "select * from `t_c_union_camp_war_group_union` where weekId=? and `centerGroupId` in (?)";

    public Map<Integer, Map<Integer, List<CrossUnionCampWarGroupUnionData>>> getCrossUnionCampWarGroupUnionDataMap(List<Integer> centerGroupIds, int weekId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, List<CrossUnionCampWarGroupUnionData>>> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_c_union_camp_war_group_union where weekId=? and centerGroupId in(" + StringUtils.listToString(centerGroupIds, ",") + ")";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);

            int i = 1;
            call.setInt(i++, weekId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossUnionCampWarGroupUnionData data = parse(rs);
                Map<Integer, List<CrossUnionCampWarGroupUnionData>> centerGroupMap = dataMap.get(data.getCenterGroupId());
                if (null == centerGroupMap){
                    centerGroupMap = new ConcurrentHashMap<>();
                    dataMap.put(data.getCenterGroupId(), centerGroupMap);
                }
                List<CrossUnionCampWarGroupUnionData> dataList = centerGroupMap.get(data.getGroupId());
                if (null == dataList){
                    dataList = new ArrayList<>();
                    centerGroupMap.put(data.getGroupId(), dataList);
                }
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }


    public static String updateCrossUnionCampWarGroupUnionDataSql = "update `t_c_union_camp_war_group_union` set campId=?,totalAbility=?,serverId=?,mermberPositionMap=? where centerGroupId=? and groupId=? and unionId=? and weekId=?";
    @Override
    protected boolean updateData(CrossUnionCampWarGroupUnionData crossUnionCampWarGroupUnionData) {
        crossUnionCampWarGroupUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossUnionCampWarGroupUnionData.getCampId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossUnionCampWarGroupUnionData.getTotalAbility().toString()));
        para.put(i++, new DbParameter(Types.BIGINT, crossUnionCampWarGroupUnionData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossUnionCampWarGroupUnionData.getMermberPositionMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossUnionCampWarGroupUnionData.getCenterGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossUnionCampWarGroupUnionData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossUnionCampWarGroupUnionData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossUnionCampWarGroupUnionData.getWeekId()));
        if (getDb().executeNoneQuery(updateCrossUnionCampWarGroupUnionDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossUnionCampWarGroupUnionDataSql = "insert into `t_c_union_camp_war_group_union`(centerGroupId,groupId,unionId,campId,weekId,totalAbility,serverId,mermberPositionMap) values (?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossUnionCampWarGroupUnionData crossUnionCampWarGroupUnionData) {
        crossUnionCampWarGroupUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossUnionCampWarGroupUnionData.getCenterGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossUnionCampWarGroupUnionData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossUnionCampWarGroupUnionData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossUnionCampWarGroupUnionData.getCampId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossUnionCampWarGroupUnionData.getWeekId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossUnionCampWarGroupUnionData.getTotalAbility().toString()));
        para.put(i++, new DbParameter(Types.BIGINT, crossUnionCampWarGroupUnionData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossUnionCampWarGroupUnionData.getMermberPositionMap())));
        if (getDb().executeNoneQuery(insertCrossUnionCampWarGroupUnionDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossUnionCampWarGroupUnionData parseData(ResultSet rs) throws SQLException {
        CrossUnionCampWarGroupUnionData crossUnionCampWarGroupUnionData = new CrossUnionCampWarGroupUnionData();
        crossUnionCampWarGroupUnionData.setCenterGroupId(rs.getInt("centerGroupId"));
        crossUnionCampWarGroupUnionData.setGroupId(rs.getInt("groupId"));
        crossUnionCampWarGroupUnionData.setUnionId(rs.getString("unionId"));
        crossUnionCampWarGroupUnionData.setCampId(rs.getInt("campId"));
        crossUnionCampWarGroupUnionData.setWeekId(rs.getInt("weekId"));
        String totalAbility = rs.getString("totalAbility");
        if (!StringUtils.isNullOrEmpty(totalAbility)){
            crossUnionCampWarGroupUnionData.setTotalAbility(new BigInteger(totalAbility));
        }
        crossUnionCampWarGroupUnionData.setServerId(rs.getLong("serverId"));
        crossUnionCampWarGroupUnionData.setMermberPositionMap(JSON.parseObject(rs.getString("mermberPositionMap"), new TypeReference<java.util.Map<java.lang.Long, java.lang.Integer>>() {}));

        return crossUnionCampWarGroupUnionData;
    }

}
